//
//  QJMineVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/30.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineVC.h"
#import "QJMineHeaderView.h"
#import "QJMineTableViewCell.h"
#import "QJMineJumpVCTool.h"
#import "QJLoginVC.h"
//我的订单
#import "QJMineOrderPageVC.h"
//我的收益
#import "QJMineNewShouYiVC.h"
//我的收藏
#import "QJMineCollectVC.h"
//收款二维码
#import "QJShouKuanVC.h"
//银行卡
#import "QJMineBankVC.h"
//地址管理
#import "QJMineAddressVC.h"
//邀请注册
#import "QJMineInviteVC.h"
//推荐关系
#import "QJTuiJianVC.h"
//开店
#import "QJKaiDianVC.h"
#import "YFKaiDianVC.h"
#import "YFUserStatusAndTypeTool.h"
#import "YFShopkeeperInformationVC.h"
#import "YFBrowseRecordsVC.h"
#import "YFCityPartnerViewController.h"

#import "ModifyApayVC.h"

@interface QJMineVC () <UITableViewDelegate,UITableViewDataSource>
{
    // -1  未找到店铺(新用户注册就是这个)  0 未审核  1 审核通过  2 审核未通过
    NSInteger _userState; // 这个字段是针对申请商户而言
    
    NSInteger _user_type;  // 1 是 泉界会员，2 是泉界创客，3，是泉界商家
}
@property (nonatomic, strong) UITableView *mineTableView;
@property (nonatomic, strong) QJMineHeaderView *headerView;
@property (nonatomic, strong) UserInfoModel *model;
@property (nonatomic, strong) QJMineJumpVCTool *jumpTool;
@end

@implementation QJMineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    [self setupUIAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //去除导航栏下方的横线
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:YFColor_Red]
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    
//    _userState = -1; // 网络不好,默认不显示
//    [self setNavAlpha:YES];
    UserInfoModel *model = [QJUserInfo getCurrentUser];
    [self.headerView headerUpdateWithModel:model];
    [self.mineTableView reloadData];
    
    /*
     用户类型新接口判断:
     1.开店 1处
     2.我的 3处  numberOfRowsInSection/didSelectRowAtIndexPath/cell里面
     3.我的详情页
     4.收款
     */
    // 用户类型, 用新接口判断
    [YFUserStatusAndTypeTool fetchUserStatus:^(NSInteger status, NSInteger userType) {
        _userState = status;
        _user_type = userType;
        
        [self.mineTableView reloadData];
        
    }];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self setNavAlpha:NO];
}
#pragma mark -- 界面布局
- (void)setupUIAction{
    WEAKSELF
    self.headerView = [[[NSBundle mainBundle]loadNibNamed:@"QJMineHeaderView" owner:self options:nil] lastObject];
    self.headerView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 250);
    self.headerView.headerBtnClikBlock = ^(NSInteger tag) {
        NSString *tokenStr = [QJUserInfo getToken];
        if (tokenStr.length < 1) {
            QJLoginVC *VC = [[QJLoginVC alloc]init];
            [weakSelf.navigationController pushViewController:VC animated:YES];;
            return;
        }
        switch (tag) {
            case 200:
            {
                //个人信息
                [weakSelf.jumpTool jumpToLoginOrInformationAction];
            }
                break;
            case 201:
            {
                //查看全部订单
                QJMineOrderPageVC *VC = [[QJMineOrderPageVC alloc]init];
                VC.htopViewBtnTag = 0;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 202:
            {
                //待付款
                QJMineOrderPageVC *VC = [[QJMineOrderPageVC alloc]init];
                VC.htopViewBtnTag = 1;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 203:
            {
                //待发货
                QJMineOrderPageVC *VC = [[QJMineOrderPageVC alloc]init];
                VC.htopViewBtnTag = 2;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 204:
            {
                //已发货
                QJMineOrderPageVC *VC = [[QJMineOrderPageVC alloc]init];
                VC.htopViewBtnTag = 3;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 205:
            {
                //已完成
                QJMineOrderPageVC *VC = [[QJMineOrderPageVC alloc]init];
                VC.htopViewBtnTag = 4;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 206:
            {
                
            }
            default:
                break;
        }
    };
    
    
    CGFloat top = IOS_VERSION < 11.0 ? Height_NavBar : 0;
    self.mineTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, UISCREEN_HEIGHT - top - Height_TabBar) style:UITableViewStylePlain];
    [self.view addSubview:self.mineTableView];
    self.mineTableView.tableHeaderView = self.headerView;
    self.mineTableView.delegate = self;
    self.mineTableView.dataSource = self;
    self.mineTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    [self.mineTableView setBackgroundColor:[UIColor blueColor]];
    self.mineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    /*
     不透明的bar情况下,也进行视图延伸.
     */
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (IOS_VERSION < 11.0) {
        //解决表无故偏移
        self.automaticallyAdjustsScrollViewInsets = NO;
    }else {
//        if (@available(iOS 11.0, *)) {
//            self.mineTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        } else {
//            // Fallback on earlier versions
//        }
    }
    
}
#pragma mark -- 数据请求方法
/*
 "我的"界面, "收款二维码"显示逻辑, 再次确定:
 老接口: user_type
 1 是 泉界会员，2 是泉界创客，3，是泉界商家
 
 新接口: state
 -1 未找到店铺   新用户注册就是这个
 0 未审核    都是’’成为创客”
 1 审核通过  “收款二维码”
 2 审核未通过  —>  再看user_type   1 是 泉界会员，2 是泉界创客，3，是泉界商家
 */

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        /***********************
         改动的地方:
         1.numberOfRowsInSection
         2.didSelectRowAtIndexPath
         3.cell里面
         **********************/
        
        // 新接口 逻辑思路
        if (_userState == 0 || _userState == -1) { // “成为创客”
            return 4;
        }else if (_userState == 1) {  // “收款二维码”
            return 5;
        }else if (_userState == 2) {
            
            if (_user_type == 1) { // 泉界会员
                return 5;   // 都显示”成为创客”
            }else if (_user_type == 2) { // 泉界创客
                return 4;  //  什么都不显示
            }else {  // 3，是泉界商家 不可能有这种情况, 跟 _userState == 2 审核未通过 矛盾
                return 4;  //  什么都不显示
            }

        }else {
            return 4;
        }
    }else{
        if (_userState == 0 || _userState == -1) { // “成为创客”
            
            return 6;
            
        }
        return 8;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cellID";
    QJMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QJMineTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.userState = _userState;
    [cell cellUpdateWithIndex:indexPath];
    
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *tokenStr = [QJUserInfo getToken];
    if (tokenStr.length < 1) {
        QJLoginVC *VC = [[QJLoginVC alloc]init];
        KPushViewControllerAction(VC);
        return;
    }
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0: // 我的收益
                {
                    QJMineNewShouYiVC *VC = [[QJMineNewShouYiVC alloc]init];
                    KPushViewControllerAction(VC);
                }
                    break;
                case 1: // 我的收藏
                {
                    QJMineCollectVC *VC = [[QJMineCollectVC alloc]init];
                    KPushViewControllerAction(VC);
                }
                    break;
                case 2: //  我的足迹
                {
                    YFBrowseRecordsVC *VC = [[YFBrowseRecordsVC alloc]init];
                    KPushViewControllerAction(VC);
                }
                    break;
                case 3: // 银行卡
                {
                    QJMineBankVC *VC = [[QJMineBankVC alloc]init];
                    VC.typeStr = @"mine";
                    KPushViewControllerAction(VC);
                    [MBPHUD showAlertWithTitle:@"暂未开放"];
                }
                    break;
                case 4:
                {
                    if (_userState == 0) { // 是’’成为创客”
                        //成为创客
                        [self applyChuangKeAction];
                    }else if (_userState == 1) { // “收款二维码”
                        //收款二维码
                        QJShouKuanVC *VC = [[QJShouKuanVC alloc]init];
                        KPushViewControllerAction(VC);
                        
                    }else if (_userState == 2) {
                        UserInfoModel *model = [QJUserInfo getCurrentUser];
                        if ([model.user_type isEqualToString:@"1"] || [model.user_type isEqualToString:@"3"]) {
                            // 都显示”成为创客”
                            [self applyChuangKeAction];
                        }else {
                            //  什么都不显示
                        }
                    }
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        
        case 1:
        {
            switch (indexPath.row) {
                case 0: //推荐关系
                {
                    QJTuiJianVC *VC = [[QJTuiJianVC alloc]init];
                    KPushViewControllerAction(VC);
                }
                    break;
                case 1: //地址管理
                {
                    QJMineAddressVC *VC = [[QJMineAddressVC alloc]init];
                    VC.typeStr = @"mine";
                    KPushViewControllerAction(VC);
                }
                    break;
                case 2: //邀请注册
                {
                    NSString *userType = [QJUserInfo getUserType];
                    if ([userType isEqualToString:@"1"]) {
                        [MBPHUD showAlertWithTitle:@"您是泉界会员不能进行邀请用户!"];
                    }else{
                        QJMineInviteVC *VC = [[QJMineInviteVC alloc]init];
                        KPushViewControllerAction(VC);
                    }
                }
                    break;
                case 3: //联系客服
                {
                    [YFGlobalTool callWithPhoneNumber:@"027-59306035"];
                }
                    break;
               
                    
                case 4: // 城市合伙人
                {
                    [MBPHUD showAlertWithTitle:@"暂未开放"];
//                    YFCityPartnerViewController *VC = [[YFCityPartnerViewController alloc]init];
//                    KPushViewControllerAction(VC);
                    
                }
                    break;
                case 5: // 修改支付宝账户
                {
                    ModifyApayVC *VC = [[ModifyApayVC alloc]init];
                    KPushViewControllerAction(VC);
                    
                }
                    break;
                    
                case 6: // 申请开店
                {
                    [WWNetRequest fetchUserTypeWithParameters:nil success:^(id response) {
                        
                        if (response) {
                            
                            NSDictionary *dict = [response objectForKey: @"data"];
                            int status  =  [[dict objectForKey:@"status"]intValue];//0未审核 1 通过审核 2 未通过审核
                            if (status == 0) {
                                
                                [MBPHUD showAlertWithTitle:@"正在审核中"];
                                
                                return ;
                            }
                            if (status == 1) {
                                [MBPHUD showAlertWithTitle:@"已通过审核"];
                                
                                return;
                            }
                            if (status == 2) {
                                YFKaiDianVC *VC = [[YFKaiDianVC alloc]init];
                                VC.type = YFKaiDianVCType_KaiDian;
                                KPushViewControllerAction(VC);
                                
                            }
                            
                        }
                        
                    } failure:^(NSDictionary *error) {
                        
                        
                        if ([[error objectForKey:@"msg"]isEqualToString:@"未发现店铺!"]) {
                            
                            YFKaiDianVC *VC = [[YFKaiDianVC alloc]init];
                            
                            VC.type = YFKaiDianVCType_KaiDian;
                            
                            KPushViewControllerAction(VC);
                            
                        }
                        
                        
                        
                    }];
                    // YFKaiDianVC  QJKaiDianVC
                    
                }
                    break;
                case 7: // 商户信息  QJKaiDianVC
                {
                    //                    [MBPHUD showAlertWithTitle:@"暂未开放"];
                    YFShopkeeperInformationVC *VC = [[YFShopkeeperInformationVC alloc]init];
                    KPushViewControllerAction(VC);
                    
                    //                    QJKaiDianVC *VC = [[QJKaiDianVC alloc]init];
                    //                    KPushViewControllerAction(VC);
                    
                }
                    break;
            }
            
        }
            break;
            
        default:
            break;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        UIView *heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 10)];
        heardView.backgroundColor = HexRGB(0xe5e5e5);
        return heardView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.0f;
    }else{
        return 10.0f;
    }
}
#pragma mark -- 申请成为创客
- (void)applyChuangKeAction{
    
    
    
    [WWNetRequest fetchApplyForMakeWithParameters:nil success:^(id response) {
        
        if (response) {
            
            [MBPHUD showAlertWithTitle:@"成为创客申请成功!"];
            
        }
        
    } failure:^(NSDictionary *error) {
        
        [MBPHUD showAlertWithTitle:@"成为创客申请失败!"];
        
    }];
}

#pragma mark -- 懒加载
-(QJMineJumpVCTool *)jumpTool{
    if (_jumpTool == nil) {
        _jumpTool = [QJMineJumpVCTool new];
        _jumpTool.mineVC = self;
    }
    return _jumpTool;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
